package com.mcr.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.reggie.common.R;
import com.mcr.reggie.dto.DishDto;
import com.mcr.reggie.entity.Dish;
import com.mcr.reggie.service.CategoryService;
import com.mcr.reggie.service.DishFlavorService;
import com.mcr.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@Api(tags = "菜品接口")
@RestController
@RequestMapping(value = "/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    /*
    *
    * **********************************   后台管理   ****************************************
    *
    * */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    public R<String> saveDish(@RequestBody DishDto dishDto){
        dishService.saveDishAndFlavor(dishDto);
        return R.success("保存成功！！");
    }
    @ApiOperation(value = "获取分页数据")
    @GetMapping(value = "/page")
    public R<Page> page(@RequestParam(value = "page",required = true)int page,
                        @RequestParam(value = "pageSize",required = true)int pageSize,
                        @RequestParam(value = "name",required = false)String name){
        Page<DishDto> dishDtoPage=dishService.getPage(page,pageSize,name);
        if(dishDtoPage==null){
            return R.error("没有这种菜品哦");
        }
        return R.success(dishDtoPage);
    }
    @ApiOperation(value = "根据id查询菜品信息和口味信息")
    @GetMapping(value = "/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getDishAndFlavor(id);
        return R.success(dishDto);
    }
    @ApiOperation(value = "修改菜品数据")
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateDishAndFlavor(dishDto);
        return R.success("修改成功");
    }
    @ApiOperation(value = "用于修改一个或多个菜品状态（停售|启售）")
    @PostMapping(value = "/status/{status}")
    public R<String> updateStatus(Long[] ids,@PathVariable("status")Integer status){
        dishService.updateDishStatus(ids,status);
        String msg;
        if(status==0){
            msg="商品停售成功";
        }else {
            msg="商品起售成功";
        }
        return R.success(msg);
    }
    @ApiOperation(value = "删除一个或多个菜品")
    @DeleteMapping
    public R<String> delByIds(Long[] ids){
        dishService.delByIds(ids);
        return R.success("删除成功");
    }
    /*
     *
     * **********************************   前台用户   ****************************************
     *
     */
    @ApiOperation(value = "前台用户展示,后台新建|修改套餐时添加菜品前展示菜品用，相当于（前后台通用）")
    @GetMapping(value = "/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtoList=dishService.getList(dish);
        return R.success(dishDtoList);
    }
}
